home *** CD-ROM | disk | FTP | other *** search
- Path: wapping.ecs.soton.ac.uk!tl93
- From: tl93@ecs.soton.ac.uk (Tony Lofthouse)
- Newsgroups: comp.lang.c++
- Subject: A template question...
- Date: 6 Mar 1996 17:57:57 GMT
- Organization: Electronics and Computer Science, University of Southampton
- Message-ID: <4hkjn5$mp1@wapping.ecs.soton.ac.uk>
- NNTP-Posting-Host: avon.ecs.soton.ac.uk
- X-Newsreader: NN version 6.5.0 (NOV)
-
- I want a generic function to perform a sort on an array, so I do the
- following:
-
- template <class T>
- void mysort(const T* anArray, unsigned arraysize) {
- // implementation stuff
- }
-
- o.k. fine. But I also want to pass a pointer to a predicate function which
- does the comparasion of elements in the array. It seems to me that this
- predicate function needs to be a template function as well:
-
- template <class T>
- unsigned greaterthan(const T& x, const T& y) {
- return x > y;
- }
-
- so my implementation of mysort is:
-
-
- template<class T>
- void mysort(const T* anArray, unsigned arraylen,
- unsigned (*predicate) (const T&, constT&)) {
-
- // sort stuff
- if(predicate( el1, el2 ) {
-
- // ----
-
- }
- //more sort stuff
-
- }
-
- Unfortunately when I do:
-
- extern int* anArray;
- extern unsigned anArrayLength;
-
- mysort(anArray, anArrayLength, &greaterthan);
-
- The compiler fails to match the instantiated mysort to the implemented
- mysort. What's going wrong?? And, how can I fix it? Any help would be
- deeply appreciated.
-
- Tony
-